/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac3.persistence.dao.impl;

import edu.uoc.tdp.pac3.configuration.PropertiesConfiguration;
import edu.uoc.tdp.pac3.domain.User;
import edu.uoc.tdp.pac3.persistence.dao.IUserDao;
import edu.uoc.tdp.pac3.persistence.exceptions.ErrorConnexioBD;
import edu.uoc.tdp.pac3.persistence.exceptions.ErrorSQL;
import edu.uoc.tdp.pac3.persistence.exceptions.ItemNoTrobat;
import java.beans.Statement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gerard
 */
public class UserDao extends BaseDao<User> implements IUserDao {

    public UserDao() {
           super(PropertiesConfiguration.getInstance(), "tb_user");
    }
    
    @Override
    public List<User> findAll() {
        List<User> list = null;
        try {
            openConnection();
            java.sql.Statement st = connexio.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM " + getTableName() + " order by user_name");
            list = new ArrayList<User>();
            while (rs.next()) {
                User user = new User();
                user.setCreationDate(rs.getDate("creation_date"));
                user.setId(rs.getInt("user_id"));
                user.setName(rs.getString("user_name"));
                list.add(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return list;
    }

    @Override
    public boolean insert(User entity) {
        boolean result = false;
        try {
            openConnection();
            begin();
            StringBuilder sb = new StringBuilder();            
            sb.append("INSERT INTO ").append(getTableName()).append(" VALUES(?,?,?)");
            PreparedStatement statement = connexio.prepareStatement(sb.toString());

            if(entity.getId() != null){
                statement.setInt(1, entity.getId());
            }
            
            if(entity.getName() != null){
                statement.setString(2, entity.getName());
            }
            
            if(entity.getCreationDate() != null){
                statement.setDate(3, new Date(entity.getCreationDate().getTime()));
            }
            
            int i = statement.executeUpdate();
            if (i > 0) {
                result = true;
            }
            commit();
        } catch (ErrorSQL ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return result;
    }

    @Override
    public boolean update(User entity) {
         boolean result = false;
        try {
            openConnection();
            begin();
            StringBuilder sb = new StringBuilder();
            sb.append("UPDATE ").append(getTableName()).append("SET ")  .append("user_name=?, ")
                                                                        .append("creation_date=? ")
                                                       .append(" WHERE user_id=?");
            
            PreparedStatement statement = connexio.prepareStatement(sb.toString());
            
            if(entity.getName() != null){
                statement.setString(1, entity.getName());
            }
            
            if(entity.getCreationDate() != null){
                statement.setDate(2, new Date(entity.getCreationDate().getTime()));
            }
            
            statement.setInt(3, entity.getId());

            int i = statement.executeUpdate();
            if (i > 0) {
                result = true;
            }
            commit();
        } catch (ErrorSQL ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return result;
    }

    @Override
    public boolean delete(User entity) {
               boolean result = false;
        try {
            openConnection();
            begin();
            StringBuilder sb = new StringBuilder();
            sb.append("DELETE FROM ").append(getTableName()).append(" WHERE user_id=?");
            PreparedStatement statement = connexio.prepareStatement(sb.toString());
            statement.setInt(1, entity.getId());
            int i = statement.executeUpdate();
            if (i > 0) {
                result = true;
            }
            commit();
        } catch (ErrorSQL ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return result;
    }

    @Override
    public User findById(Integer id) {
        User user = null;
        try {
            openConnection();
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT * FROM ").append(getTableName()).append(" WHERE user_id=?");
            PreparedStatement statement = connexio.prepareStatement(sb.toString());
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setCreationDate(rs.getDate("creation_date"));
                user.setId(rs.getInt("user_id"));
                user.setName(rs.getString("user_name"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return user;
    }
    
}
